/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * Copyright 2007 MartiniMix.net
 */
package net.martinimix.logging;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogConfigurationException;
import org.apache.commons.logging.LogFactory;

/**
 * Provides a Blue Martini log factory for Apache Common's Logging.
 * 
 * @author Scott Rossillo
 *
 */
public class BlueMartiniLogFactory extends LogFactory {
	
	/**
	 * True if internal debugging is enabled.
	 * To enable, add -Dnet.martinimix.logging.debug=true to java's
	 * command line arguments.
	 */
	protected final static boolean DEBUG_INTERNAL_ENABLED = 
		Boolean.getBoolean("net.martinimix.logging.debug");
	
	private final Map attributes = new HashMap();
	
	/**
	 * Constructs a new Blue Martini logging factory.
	 */
	public BlueMartiniLogFactory() {
		super();
	}

	/* (non-Javadoc)
	 * @see org.apache.commons.logging.LogFactory#getAttribute(java.lang.String)
	 */
	public Object getAttribute(String key) {
		
		return attributes.get(key);
	}

	/* (non-Javadoc)
	 * @see org.apache.commons.logging.LogFactory#getAttributeNames()
	 */
	public String[] getAttributeNames() {
		
		return (String[]) attributes.keySet().toArray(new String [0]);
	}

	/* (non-Javadoc)
	 * @see org.apache.commons.logging.LogFactory#getInstance(java.lang.Class)
	 */
	public Log getInstance(Class clazz) throws LogConfigurationException {
		
		return this.getInstance(clazz.getName());
	}

	/* (non-Javadoc)
	 * @see org.apache.commons.logging.LogFactory#getInstance(java.lang.String)
	 */
	public Log getInstance(String clazz) throws LogConfigurationException {
		
		return createInstance(clazz);
	}
	
	private BlueMartiniLog createInstance(String clazz) {
		
		BlueMartiniLog log =  new BlueMartiniLog(clazz);
		log.setLogFactory(this);
		
		if(LogStartupCallback.isInitialiazed()) {
			if(BlueMartiniLogFactory.DEBUG_INTERNAL_ENABLED) {
				System.err.println("Intializing log for " + clazz);
			}
			log.initLog();
		} else {
			if(BlueMartiniLogFactory.DEBUG_INTERNAL_ENABLED) {
				System.err.println("Deferring init for log " + clazz);
			}
			// bm logging is fuxored until app finishes starting up, so wait
			LogStartupCallback.addLogForInitialization(log);
		}
		
		return log;
	}

	/* (non-Javadoc)
	 * @see org.apache.commons.logging.LogFactory#release()
	 */
	public void release() {
		/* not needed */
	}

	/* (non-Javadoc)
	 * @see org.apache.commons.logging.LogFactory#removeAttribute(java.lang.String)
	 */
	public void removeAttribute(String key) {

		attributes.remove(key);
	}

	/* (non-Javadoc)
	 * @see org.apache.commons.logging.LogFactory#setAttribute(java.lang.String, java.lang.Object)
	 */
	public void setAttribute(String key, Object value) {
		
		attributes.put(key, value);
	}

}
